// PointZoomOutCommand.cpp: implementation of the PointZoomOutCommand class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "PointZoomOutCommand.h"
#include <dbsymtb.h>
#include <geassign.h>
#include <migrtion.h>
#include <math.h>
#include <dbents.h>
#include <aced.h>
#include "UEEntityOperation.h"
#include "dbxutil.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

PointZoomOutCommand::PointZoomOutCommand(const AcGePoint3d& pt, DOUBLE dbHeight, DOUBLE dbWidth)
{
	m_pt = pt;
	ASSERT(dbHeight>0.0);
	ASSERT(dbWidth >0.0);

	m_dbHeight = dbHeight;
	m_dbWidth = dbWidth;
}

PointZoomOutCommand::~PointZoomOutCommand()
{

}

BOOL PointZoomOutCommand::Execute()
{
	struct resbuf OriginalRb;
	struct resbuf SetRb;

	acedGetVar(_T("CMDECHO"), &OriginalRb);

	SetRb.restype = RTSHORT;
	SetRb.resval.rint = 0;
	acedSetVar(_T("CMDECHO"), &SetRb);
	acdbWcs2Ucs(asDblArray(m_pt), asDblArray(m_pt), FALSE);
	int nResult = acedCommand(RTSTR, _T("Zoom"), RTSTR, _T("C"), RT3DPOINT, asDblArray(m_pt), RTREAL, m_dbHeight, 0);
	if(nResult != RTNORM)
	{
		CString strTemp;
		strTemp.Format(_T("Zoom\nC\n%f,%f,%f\n%f\n"), m_pt.x, m_pt.y, m_pt.z, m_dbHeight);
		SendCommandToAutoCAD(strTemp);
	}
	acedSetVar(_T("CMDECHO"), &OriginalRb);
/*	AcGePoint3d pt;
	struct resbuf fromrb, torb; 
	
	fromrb.restype = RTSHORT; 	
	fromrb.resval.rint = 0; // WCS  
	
	torb.restype = RTSHORT; 	
	torb.resval.rint = 2; // DCS  
	
	INT nResult = acedTrans(asDblArray(m_pt), &fromrb, &torb, FALSE, asDblArray(pt));
	
	AcDbViewTableRecord* pVTableRecord = new AcDbViewTableRecord;
	pVTableRecord->setCenterPoint(AcGePoint2d(pt.x, pt.y));
	pVTableRecord->setTarget(GetDCSTarget());
	pVTableRecord->setViewDirection(GetCurDirection());
	pVTableRecord->setElevation(GetCurElevation());
	pVTableRecord->setHeight(m_dbHeight);
	pVTableRecord->setWidth(m_dbWidth);
	
	if(!SetUCS(pVTableRecord))
	{
		ASSERT(FALSE);
		delete pVTableRecord;
		return FALSE;
	}

	Acad::ErrorStatus es = acedSetCurrentView(pVTableRecord, NULL);
	if(es != Acad::eOk)
	{
		ASSERT(FALSE);
		delete pVTableRecord;
		return FALSE;
	}
	delete pVTableRecord;
*/
	return TRUE;
}

AcGeVector3d PointZoomOutCommand::GetCurDirection()
{
	struct resbuf dirrb;

	acedGetVar(_T("viewdir"), &dirrb);
	ASSERT(dirrb.restype == RT3DPOINT);

	return asVec3d(dirrb.resval.rpoint);
}

AcGePoint3d PointZoomOutCommand::GetDCSTarget()
{
	struct resbuf dirrb;

	acedGetVar(_T("target"), &dirrb);
	ASSERT(dirrb.restype == RT3DPOINT);

	return asPnt3d(dirrb.resval.rpoint);
}

DOUBLE PointZoomOutCommand::GetCurElevation()
{
	struct resbuf dirrb;

	acedGetVar(_T("ELEVATION"), &dirrb);
	ASSERT(dirrb.restype == RTREAL);

	return dirrb.resval.rreal;
}

BOOL PointZoomOutCommand::SetUCS(AcDbViewTableRecord* pRecord)
{
	Acad::ErrorStatus es;
	AcGeMatrix3d matrixUCS;
	es = acedGetCurrentUCS(matrixUCS);
	if(es != Acad::eOk)
	{
		ASSERT(FALSE);
		return FALSE;
	}

	AcGePoint3d OrginalPt;
	AcGeVector3d vX;
	AcGeVector3d vY;
	AcGeVector3d vZ;

	matrixUCS.getCoordSystem(OrginalPt, vX, vY, vZ);
	es = pRecord->setUcs(OrginalPt, vX, vY);
	if(es != Acad::eOk)
	{
		ASSERT(FALSE);
		return FALSE;
	}
	return TRUE;
}